package com.itheima.customer.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.customer.dao.ILinkManDao;
import com.itheima.customer.domain.LinkMan;
import com.itheima.customer.service.ILinkManService;
import com.itheima.customer.web.commens.PageBean;

@Service("linkManService")
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)//针对查找型方法(全局)
public class linkmanServiceImpl implements ILinkManService{

	@Autowired
	private  ILinkManDao  linkManDao;
	
 	@Override
 	@Transactional(readOnly=false,propagation=Propagation.REQUIRED) //针对非查找型方法(局部)
	public void addLinkman(LinkMan linkman) {
 		linkManDao.addLinkMan(linkman);
	}

	@Override
	public PageBean findAllLinkmans(DetachedCriteria dc ,String num) {
		
		int currentPageNum = 1;
		int totalRecords = linkManDao.findTotalRecords(dc);
		if(StringUtils.isNotBlank(num)){
			currentPageNum = Integer.parseInt(num);
		}
		PageBean pb = new PageBean(currentPageNum, totalRecords);
		 List<LinkMan> records = linkManDao.findAll(dc ,pb.getStartIndex() ,pb.getPageSize());
		 pb.setRecords(records);
		 return pb;
	}

	@Override
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED) //针对非查找型方法(局部)
	public void removeLinkman(LinkMan linkman) {
		
		linkManDao.remove(linkman);
		
	}

	@Override
	public LinkMan findLinkmanById(Long lkmId) {
		
		return  linkManDao.findById(lkmId);
	}

	@Override
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED) //针对非查找型方法(局部)
	public void updateLinkman(LinkMan linkman) {
		
		linkManDao.update(linkman);
	}

}
